<template>
  <div class="search">
    <f-form ref="form" :action="api.coupon" :form-key="this.formItem.couponId" :file-ids="fileIds">
      <f-form-item label="优惠券标题" name="couponTitle" :required="true" :value="formItem.couponTitle"/>
      <f-form-item label="优惠券面值" name="couponAmount" type="number" :required="false" :value="formItem.couponAmount"/>
      <f-form-item label="发放数量" name="provideNum" type="number" :required="false" :value="formItem.provideNum"/>
     <!-- <f-form-item label="限领数量" name="receiveNum" type="number" :required="false" :value="formItem.receiveNum"/>-->
      <f-form-item label="剩余数量" name="surplusNum" type="number" :required="false" :value="formItem.surplusNum"/>
      <f-form-item label="开始日期" dataType="object" type="date" name="beginDate1" :required="true" :value="$moment(formItem.expiryBeginTime)" @change="beginDateChange"/>
      <f-form-item label="结束日期" dataType="object" type="date" name="endDate1" :required="true" :value="$moment(formItem.expiryEndTime)" @change="endDateChange"/>
      <f-form-item label="优惠形式" name="couponType" type="select" :required="true" data-type="number" :data="couponTypeData" :value="formItem.couponType" label-key="label" value-key="value"/>
      <f-form-item label="使用门槛" name="withAmount" type="number"  :required="true" :value="formItem.withAmount"/>
    </f-form>
  </div>
</template>

<script>
  export default {
    name: 'couponForm',
    props: {},
    data() {
      return {
        isAdd: true,//是否是添加模式
        loaded: false,//是否加载轮播图组件
        fileIds: [],//上传文件ID
        formItem: this.getFormItem(),
        couponTypeData: [{ label: '满减', value: 2 }],
        withAmountData: [{ label: '不限制', value: '-1' }, { label: '满n元可用', value: '1 ' }]
      }
    },
    methods: {
      getFormItem() {
        return {
          couponId: '',
          couponTitle: '',
          expiryBeginTime: this.$moment().format('YYYY-MM-DD'),
          expiryEndTime: this.$moment().format('YYYY-MM-DD'),
          couponType: '',
          couponAmount: 0,
          withAmount: 0,
          provideNum: 0,
          receiveNum: 1
        }
      },
      beginDateChange(date) {
        this.formItem.expiryBeginTime = date
      },
      endDateChange(date) {
        this.formItem.expiryEndTime = date
      },
      //表单赋值
      setData(data) {
        this.$refs.form.resetFields()
        this.formItem = this.getFormItem()
        this.isAdd = true
        if (data && data.couponId) {
          this.$refs.form.getDetails({ couponId: data.couponId }, (data) => {
            this.formItem = data
            this.loaded = true
          })
          this.isAdd = false
        } else {
          this.loaded = true
        }
      },
      //文件上传回调
      uploadSuccess(fileIds, files) {
        this.fileIds = fileIds
      },
      //提交表单
      handleSubmit(callback) {
        this.$refs.form.submit({ expiryBeginTime: this.formItem.expiryBeginTime, expiryEndTime: this.formItem.expiryEndTime,receiveNum:1 }, callback)
      }
    },
    created() {

    },
    components: {}
  }
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
